/**
 * 请求返回结果预处理，处理登录等异常操作
 */
import { ElMessage, ElMessageBox } from "element-plus";
// import { useUserStore } from "@/store/modules/user";
import ErrorCode from "./error-code";

export function ResponseCode(res) {
  let message = ErrorCode[res.code] || res.msg;
  //   const userStore = useUserStore();
  if (res.code === 10001) {
    // 3 已经登出 10001 token已经过期了
    ElMessageBox.confirm("您的账号已经登出, 请重新登录！", "确认登出", {
      confirmButtonText: "重新登录",
      cancelButtonText: "取消",
      type: "warning"
    }).then(() => {
      /* userStore.resetToken().then(() => {
        location.href = "/login";
      }); */
    });
  } else if (res.code !== 200) {
    ElMessage({
      message: message,
      type: "error",
      duration: 3 * 1000
    });
  }
}

export function ResponseError(error) {
  let { message } = error;
  if (message == "Network Error") {
    message = "后端接口连接异常";
  } else if (message.includes("timeout")) {
    message = "系统接口请求超时";
  } else if (message.includes("Request failed with status code")) {
    message = "系统接口" + message.substr(message.length - 3) + "异常";
  }
  ElMessage({
    message: message,
    type: "error",
    duration: 3 * 1000
  });
  return Promise.reject(error);
}
